package com.test.lotterysystem.Service.Impl;

import com.test.lotterysystem.Comment.ErrorCode.ServiceErrorCode;
import com.test.lotterysystem.Comment.Exception.ServiceException;
import com.test.lotterysystem.Comment.Utils.RegexUtil;
import com.test.lotterysystem.Comment.Utils.SecurityUtil;
import com.test.lotterysystem.Controller.Param.UserRegisterParam;
import com.test.lotterysystem.Controller.Result.UserRegisterResult;
import com.test.lotterysystem.Dao.DaoObject.Encrypt;
import com.test.lotterysystem.Dao.DaoObject.UserDO;
import com.test.lotterysystem.Dao.Mapper.UserMapper;
import com.test.lotterysystem.Service.DTO.UserRegisterDTO;
import com.test.lotterysystem.Service.Enum.UserIdentityEnum;
import com.test.lotterysystem.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserRegisterDTO register(UserRegisterParam userRegisterParam) {
        // 检测数据是否符合条件
        checkRegisterInfo(userRegisterParam);

        // 创建对象
        UserDO userDO = new UserDO();
        userDO.setUserName(userRegisterParam.getName());
        userDO.setEmail(userRegisterParam.getMail());
        userDO.setPhoneNumber(new Encrypt(userRegisterParam.getPhoneNumber()));
        if(StringUtils.hasText(userRegisterParam.getPassword())){
            userDO.setPassword(SecurityUtil.encrypt(userRegisterParam.getPassword()));
        }
        userDO.setIdentity(userRegisterParam.getIdentity());

        // 进行插入
        userMapper.insert(userDO);

        UserRegisterDTO userRegisterDTO = new UserRegisterDTO();
        userRegisterDTO.setUserId(userDO.getId());
        return userRegisterDTO;
    }

    private void checkRegisterInfo(UserRegisterParam userRegisterParam) {
        // 注册信息为空
        if (userRegisterParam == null) throw new ServiceException(ServiceErrorCode.REGISTER_INFO_IS_EMPTY);


        // 检测邮箱
        if (!RegexUtil.checkMail(userRegisterParam.getMail()))
            throw new ServiceException(ServiceErrorCode.MAIL_IS_ERROR);

        if (mailIsUse(userRegisterParam.getMail()))
            throw new ServiceException(ServiceErrorCode.MAIL_IS_USE);


        // 检测手机号
        if (!RegexUtil.checkPhone(userRegisterParam.getPhoneNumber()))
            throw new ServiceException(ServiceErrorCode.PHONE_IS_ERROR);

        if (phoneIsUse(userRegisterParam.getPhoneNumber()))
            throw new ServiceException(ServiceErrorCode.PHONE_IS_USE);


        // 检测用户身份是否正确
        if (UserIdentityEnum.checkIdentity(userRegisterParam.getIdentity()) == null)
            throw new ServiceException(ServiceErrorCode.IDENTITY_IS_ERROR);


        // 检测密码
        if (userRegisterParam.getIdentity().equalsIgnoreCase(UserIdentityEnum.ADMIN.name())
                && !StringUtils.hasText(userRegisterParam.getPassword())) {
            throw new ServiceException(ServiceErrorCode.PASSWORD_IS_EMPTY);
        }

        if (StringUtils.hasText(userRegisterParam.getPassword())
                && !RegexUtil.checkPassword(userRegisterParam.getPassword())) {
            throw new ServiceException(ServiceErrorCode.PASSWORD_ERROR);
        }


    }

    private boolean phoneIsUse(String phoneNumber) {
        return userMapper.phoneIsUse(phoneNumber) > 0;
    }

    private boolean mailIsUse(String mail) {
        return userMapper.mailIsUse(mail) > 0;
    }
}
